package k1;

import java.util.HashMap;


public class ComputerPlayer extends Player{
	/**
	 * table row count
	 */
	private int tableRows = 3;
	/**
	 * describes computer player
	 */
	public ComputerPlayer(){
		setName("Arvuti");
		setMark("O");
	}
	/**
	 * function that makes AI's move
	 * check rows, columns and diagonals separately
	 * @param table
	 * @param player 
	 */
	//first checks rows, then columns and then diagonals...if no threat found, makes neutral move.
	public void Move(HashMap<Integer, String> table){
		for(int i = 0;i<table.size();i++){
			if(table.get(i) != null && table.get(i+1) != null && table.get(i+2) == null &&
					(!table.get(i).equals(getMark()) && !table.get(i+1).equals(getMark()) || 
							table.get(i).equals(getMark()) && table.get(i+1).equals(getMark()))){
				table.put(i+2, getMark());
				return;
			}else if(table.get(i+1) != null && table.get(i+2) != null && table.get(i) == null &&
					(!table.get(i+1).equals(getMark()) && !table.get(i+2).equals(getMark())|| 
							table.get(i+1).equals(getMark()) && table.get(i+2).equals(getMark()))){
				table.put(i, getMark());
				return;
			}else if(table.get(i) != null && table.get(i+2) != null && table.get(i+1) == null &&
					(!table.get(i).equals(getMark()) && !table.get(i+2).equals(getMark())|| 
							table.get(i).equals(getMark()) && table.get(i+2).equals(getMark()))){
				table.put(i+1,getMark());
				return;
			}
			i = i + 2;
		}
		//check rows if there is a necessary move available
		for(int i = 0;i<tableRows;i++){
			if(table.get(i) != null && table.get(i+3) != null && table.get(i+3+3) == null &&
					(!table.get(i).equals(getMark()) && !table.get(i+3).equals(getMark()) || 
							table.get(i).equals(getMark()) && table.get(i+3).equals(getMark()))){
				table.put(i+3+3, getMark());
				return;
			}else if(table.get(i) != null && table.get(i+3+3) != null && table.get(i+3) == null &&
					(!table.get(i).equals(getMark()) && !table.get(i+3+3).equals(getMark()) || 
							table.get(i).equals(getMark()) && table.get(i+3+3).equals(getMark()))){
				table.put(i+3, getMark());
				return;
			}else if(table.get(i+3) != null && table.get(i+3+3) != null && table.get(i) == null &&
					(!table.get(i+3).equals(getMark()) && !table.get(i+3+3).equals(getMark()) || 
							table.get(i+3).equals(getMark()) && table.get(i+3+3).equals(getMark()))){
				table.put(i, getMark());
				return;
			}
		}
		//checking both diagonals
		if(table.get(0) != null && table.get(4) != null && table.get(8) == null &&
					(!table.get(0).equals(getMark()) && !table.get(4).equals(getMark()) || 
							table.get(0).equals(getMark()) && table.get(4).equals(getMark()))){
			table.put(8, getMark());
			return;
		}else if(table.get(0) != null && table.get(8) != null && table.get(4) == null &&
				(!table.get(0).equals(getMark()) && !table.get(8).equals(getMark()) || 
						table.get(0).equals(getMark()) && table.get(8).equals(getMark()))){
			table.put(4, getMark());
			return;
		}else if(table.get(4) != null && table.get(8) != null && table.get(0) == null &&
				(!table.get(4).equals(getMark()) && !table.get(8).equals(getMark()) || 
						table.get(4).equals(getMark()) && table.get(8).equals(getMark()))){
			table.put(0, getMark());
			return;
		}else if(table.get(2) != null && table.get(4) != null && table.get(6) == null &&
				(!table.get(2).equals(getMark()) && !table.get(4).equals(getMark()) || 
						table.get(2).equals(getMark()) && table.get(4).equals(getMark()))){
			table.put(6, getMark());
			return;
		}else if(table.get(4) != null && table.get(6) != null && table.get(2) == null &&
				(!table.get(4).equals(getMark()) && !table.get(6).equals(getMark()) || 
						table.get(4).equals(getMark()) && table.get(6).equals(getMark()))){
			table.put(2, getMark());
			return;
		}else if(table.get(2) != null && table.get(6) != null && table.get(4) == null &&
				(!table.get(2).equals(getMark()) && !table.get(6).equals(getMark()) || 
						table.get(2).equals(getMark()) && table.get(6).equals(getMark()))){
			table.put(4, getMark());
			return;
		}
		//if there is no need to block anything, makes a move to middle, corner, or elsewhere.
		if(table.get(table.size()/2) == null){
			table.put(table.size()/2, getMark());
		}else if(table.get(table.size()/2) != null){
			for(int i = 0;i<table.size();i++){
				if((i % 2) == 0 && table.get(i) == null ){
					table.put(i, getMark());
					return;
				}
			}
		}else{
			for(int i = 0;i<table.size();i++){
				if(table.get(i) == null){
					table.put(i, getMark());
				}
			}
		}
	}
}
